package com.lm56.web.network.controller;

import com.lm56.common.MyRequestBody;
import com.lm56.common.MyResponseBody;
import com.lm56.common.exception.FieldValidateExcetiption;
import com.lm56.common.exception.MyException;
import com.lm56.web.network.domain.CompanyLine;
import com.lm56.web.network.domain.CompanyLineQueryForm;
import com.lm56.web.network.domain.CompanyNetwork;
import com.lm56.web.network.domain.CompanyNetworkQeuryForm;
import com.lm56.web.network.service.LinesService;
import com.lm56.web.security.SecUserDetails;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by Kelvin Yeung on 2016/12/7.
 */
@Controller
@RequestMapping(value ="/workbench")
public class NetworkController {


    Logger logger = LoggerFactory.getLogger(NetworkController.class);

    @Autowired
    LinesService linesService;


    @RequestMapping(value = "networks")
    public String networks() {
        return "workbench/company/networks";
    }

    @RequestMapping(value = "networks",method = RequestMethod.POST)
    @ResponseBody
    public MyResponseBody network(@RequestBody MyRequestBody<CompanyNetworkQeuryForm> requestBody ,BindingResult result,Model model){

        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        CompanyNetworkQeuryForm params = requestBody.getParams();

        if (params == null) {
            params = new CompanyNetworkQeuryForm();
        }

        params.setUserId(secUser.getUserId());
        requestBody.setParams(params);
        List<CompanyNetwork> networks = linesService.getNetworks(requestBody);
        return new MyResponseBody(MyResponseBody.STATUS_SUCESS, "获取网点列表成功", networks);
    }


    @RequestMapping(value = "network/add",method = RequestMethod.GET)
    public String addNetwork(){

        return "workbench/company/network_add";
    }

    @RequestMapping(value = "network/add",method = RequestMethod.POST)
    public String addNetwork(@Valid CompanyNetwork network ,BindingResult result,Model model){

        if(result.hasErrors()){
            return  "workbench/company/network_add";
        }

        try {
            linesService.addNetwork(network);
        }catch (FieldValidateExcetiption e){
            result.rejectValue(e.getField(),null,e.getMessage());
            return "workbench/company/network_add";
        }

        model.addAttribute("success", "success");
        return "workbench/company/network_add";
    }



    @RequestMapping(value = "network/detail",method = RequestMethod.GET)
    public String networkDetail(@RequestParam("id") Long networkId,Model model){
        if(networkId==null){
            throw new MyException("网点不存在");
        }
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        CompanyNetwork network = linesService.getNetworkByNetworkId(secUser.getUserId(),networkId);
        if(network ==null){
            throw new MyException("网点不存在");
        }
        model.addAttribute("companyNetwork",network);
        return "workbench/company/network_detail";
    }

    @RequestMapping(value = "network/detail",method = RequestMethod.POST)
    public String networkDetail(@Valid CompanyNetwork network ,BindingResult result,Model model){

        if(result.hasErrors()){
            return  "workbench/company/network_detail";
        }

        try {
            linesService.updateNetwork(network);
        }catch (FieldValidateExcetiption e){
            result.rejectValue(e.getField(),null,e.getMessage());
            return "workbench/company/network_detail";
        }

        model.addAttribute("success","success");
        return "workbench/company/network_detail";
    }


    @RequestMapping(value = "network/delete",method = RequestMethod.GET)
    @ResponseBody
    public MyResponseBody deleteNetwork(@RequestParam("id") Long networkId){
        try{
            linesService.deleteNetworkById(networkId);
        }catch (MyException e){
            return new MyResponseBody(MyResponseBody.STATUS_ERROR,e.getMessage());
        }
        return new MyResponseBody(MyResponseBody.STATUS_SUCESS,"删除成功");
    }
}
